﻿<!doctype html><html><head><meta charset="utf-8" />
<title>uu.storage</title>
<script src="../uuStorage.js"></script>
<script src="uu.form.js"></script>
<script>

function xwin(uu) {
    uu.id("setpair").disabled = false;
    uu.id("refresh").disabled = false;
}

function xstorage(uu, storage) {
    uu.id("backend").innerText = storage.toString();
    updateSize();
}

function getRandomKeyValuePair() {
    uu.id("key").value = "key" + ((Math.random() * 100000) | 0);
    uu.id("val").value = "val" + ((Math.random() * 100000) | 0);
}

function updateView() {
    var storage = uu("Storage");
    var hash = storage.getAll();

    uu.value(uu.id("all"), uu.json(hash));


    var pairs = storage.pairs();

    uu.text(uu.id("pairs"), pairs + " pairs");
}

function updateSize() {
    var storage = uu("Storage");
    var size = storage.size();
    var node = uu.id("size");
    var remain = size.max - size.used;

    if (remain < 0) {
        node.style.color = "red";
    } else {
        node.style.color = "black";
    }
    if (size.max === Number.MAX_VALUE) {
        uu.text(node, "used: 0bytes, max: Number.MAX_VALUE, free: unknown");
        uu.id("limittest").disabled = true;

    } else if (size.max > 1024 * 1024) {
        uu.text(node,
            uu.format("used: ?bytes(?MB), max: ?MB, remain: ?bytes(?MB)",
                      size.used,
                      (size.used / 1024 / 1024).toFixed(2),
                      (size.max  / 1024 / 1024).toFixed(2),
                      remain,
                      (remain / 1024 / 1024).toFixed(2)));

    } else {
        uu.text(node,
            uu.format("used: ?bytes, max: ?bytes, free: ?bytes",
                      size.used,
                      size.max,
                      remain));
    }
}

function getPair() {
    var storage = uu("Storage");
    var val = storage.get(uu.id("key").value);

    uu.id("val").value = val;

    updateView();
}

function setPair(rand, norefresh) {
    var storage = uu("Storage");

    if (rand) {
        getRandomKeyValuePair();
    }
    if (!storage.set(uu.id("key").value, uu.id("val").value)) {
        alert("fail");
        return;
    }
    if (!norefresh) {
        updateView();
    }
    updateSize();
}

function removeEdgePair(last) {
    var storage = uu("Storage");
    var pairs = storage.pairs();
    var key = storage.nth(last ? pairs - 1 : 0) || "", val = "";

    if (key) {
        val = storage.get(key);
    }
    uu.id("key").value = key;
    uu.id("val").value = val;

    storage.remove(key);
    updateView();
    updateSize();
}

function removePair() {
    var storage = uu("Storage");
    var key = uu.id("key").value;

    storage.remove(key);
    updateView();
    updateSize();
}

function clearAllPairs() {
    var storage = uu("Storage");

    storage.clear();
    updateView();
    updateSize();
    uu.id("setpair").disabled = false;
    uu.id("refresh").disabled = false;
}

function limitTest() {
    var storage = uu("Storage");

    clearAllPairs();

    var size = storage.size();
    var spam;
    var used = (size.max - size.used) - 80; // 80 margin

    spam = Array(used + 1).join("0");

    if (!storage.set("a", spam)) {
        updateSize();
        alert("test fail");
    } else {
        updateSize();
        alert("test ok");
        uu.id("setpair").disabled = true;
//        uu.id("refresh").disabled = true;
    }
}
</script>
</head><body>
<h1 id="backend" style="text-shadow: gray 3px 3px 3px">Plz Wait...</h1>
<input id="refresh" type="button" value="refresh" onclick="updateView(); updateSize();" />
|
<input id="setpair" type="button" value="add new pair" onclick="setPair(1, 0)" />
<input type="button" value="remove first pair" onclick="removeEdgePair(0)" />
<input type="button" value="remove last pair" onclick="removeEdgePair(1)" />
<input type="button" value="remove pair" onclick="removePair()" />
| <input type="button" value="clear all pairs" onclick="clearAllPairs()" />
<br />
<br />
key/value: <input id="key" type="text" value="key" />
<input id="val" type="text" value="val" />
<input type="button" value="update value / create new pair" onclick="setPair(0)" />
<input type="button" value="get value from key" onclick="getPair()" />
<br /><span id="pairs">unknown</span>
<br /><span id="size">unknown</span>
<br />
<textarea id="all" cols="60" rows="5"></textarea><br />
<br />
<p>test:</p>
<input id="limittest" type="button" value="limit test" onclick="limitTest()" /> →
<input type="button" value="add pair(no refresh)" onclick="setPair(1, 1)" />

</body></html>
